package com.luzhikun.dao;

import com.luzhikun.db.JDBCUtil;
import com.luzhikun.model.Student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * ClassName: StudentDao
 * Description:
 * date: 2021/4/28 21:06
 *
 * @author luzhikun
 * @since JDK 1.8
 */
public class StudentDao implements IStudentDao {
    private JDBCUtil jdbcUtil=new JDBCUtil();
    /*
     * 检查登录
     *
     * */
    @Override
    public Student selectStudentLogin(int num, String password) {

        //创建学生对象
        Student stu = null;
        //创建连接对象
        Connection connection = null;
        //创建预处理对象
        PreparedStatement pst = null;
        //创建结果集对象
        ResultSet rs = null;
        try {
            connection = jdbcUtil.getConnection();
            //编写sql
            String sql = "select id,num,PASSWORD,NAME,age from t_student where num=? and password=?";
            //动态加载sql语句
            pst = connection.prepareStatement(sql);
            //替换占位符
            pst.setInt(1, num);
            pst.setString(2, password);
            //获取结果集
            rs = pst.executeQuery();
            if (rs.next()) {

                stu = new Student();
                stu.setId(rs.getInt("id"));
                stu.setNum(rs.getInt("num"));
                stu.setPassword(rs.getString("password"));
                stu.setName(rs.getString("name"));
                stu.setAge(rs.getInt("age"));
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                JDBCUtil.closeAll(connection, pst, rs);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return stu;

    }
}
